<template>
	<view>
		<!-- 自定义导航栏 -->
		<view class="example-body">
			<uni-nav-bar :statusBar="true" rightText="发布" left-icon="arrowleft" @clickLeft="back" @clickRight="submit" />
		</view>
		<!-- 多行输入框 -->
		<view class="uni-textarea">
			<textarea v-model.trim="publish.content" placeholder="说一句话吧(*^▽^*)" />
			</view>
			<!-- 上传多图 -->
			<upload-images @uploadImg="uploadImg" />
			<!-- 弹出公告 -->
			<uni-popup :show="isShowPopup" position="middle" mode="fixed" @hidePopup="hidePopup">
				<view class="notice">
					<view class="image">
							<image src="../../static/common/addinput.png" mode="widthFix"></image>
					</view>
					<view class="text">
						<view>1、涉及黄色，政治，广告及骚扰信息</view>
						<view>2、涉及人身攻击</view>
						<view>3、留联系方式，透露他人隐私，一经核实将被封禁，情节严重者永久封禁</view>
					</view>
					<button type="primary" @tap="hidePopup">朕知道了</button>
				</view>
			</uni-popup>
		</view>
</template>

<script>
	import uniNavBar from "components/official/uni-nav-bar/uni-nav-bar.vue"
	import uploadImages from "components/common/upload-images.vue"
	import uniPopup from "components/official/uni-popup/uni-popup.vue"
	import {postMoment} from "network/publish.js"
	import {uploadFile} from "common/tools/upload.js"
	
	export default {
		data() {
			return {
				publish:{
					content:"",
					imageList:[]
				},
				fileData: {},
				isShowPopup: uni.getStorageSync("isShowPopup") || false
			};
		},
		components: {
			uniNavBar,
			uploadImages,
			uniPopup
		},
		methods: {
			// 返回
			back() {
				uni.navigateBack()
			},
			
			// 发布
			async submit() {
				if(this.publish.content.length > 0 || this.publish.imageList.length > 0){
					// 发布
					const res = await postMoment(this.publish.content)
					const token = this.$store.state.user.token
					this.fileData.tempFilePaths.forEach(async item=>{
						await uploadFile("pic", item, token, `?momentId=${res.data.insertId}`)
					})
					if(res.meta.code === 200){
						uni.showToast({
							title: "发布动态成功"
						})
						uni.navigateBack()
					}
					uni.showToast({
						title: "发布动态失败",
						icon: "none"
					})
				}else{
					uni.showToast({
						title: "未输入内容!",
						icon: "none"
					})
				}
			},
			
			// 上传图片
			uploadImg(res, imageList){
				this.fileData = res
				this.publish.imageList = imageList
			},
			
			// 隐藏广告
			hidePopup(){
				this.isShowPopup = false
				uni.setStorageSync("isShowPopup", "1")
			}
			
		},
		created() {
			if(uni.getStorageSync("isShowPopup") !== '1'){
				this.isShowPopup = true
			}else{
				this.isShowPopup = false
			}
		}
		// 监听页面返回事件
		// onBackPress(e) {
		// 	console.log("ee",e)
		// 	if(!this.publish.content && this.publish.imageList.length === 0){
		// 		return
		// 	}
		// 		uni.showModal({
		// 			content:"是否要保存为草稿",
		// 			confirmText:"保存",
		// 			cancelText:"不保存",
		// 			success: res => {
		// 				if(res.confirm){
		// 					// 保存了
		// 					uni.setStorageSync("publish",this.publish)
		// 				}
		// 				uni.navigateBack()
		// 			}
		// 		})
		// 	return
		// }
	}
</script>

<style lang="scss" scoped>
.uni-textarea{
	border: 1upx solid #EEEEEE;
}

.notice{
	width: 520upx;
		.image{
			image{
				width: 100%;
			}
		}
		button{
			margin-top: 23upx;
			background: #ffe934;
			color: #171606;
			font-weight: bold;
		}
	}
</style>
